package com.example.itshome.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.itshome.R;
import com.example.itshome.activity.CreatePostActivity;
import com.example.itshome.adapter.CircleListPageRecycleViewAdapter;
import com.example.itshome.entity.CircleEntity;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.entity.MyInteger;
import com.example.itshome.impl.ItemDecorationImpl;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.GetCirclePostRequest;
import com.example.itshome.onclick.OnClickTurnTo;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class ActivityItem2Fragment extends Fragment {
    private ImageView createPostImageView;
    private RecyclerView mRecycleView;
    private List<CircleEntity> mRecycleViewData;
    private final MyInteger pageIndex=new MyInteger();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_activity_item2, container, false);
        initFind(view);
        initButton();
        initRecycleView();
        return view;
    }

    void initFind(View v){
        createPostImageView=v.findViewById(R.id.imv_activity_page_create_post);
        mRecycleView=v.findViewById(R.id.rcv_circle_list);
    }

    void initButton(){
        createPostImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(),CreatePostActivity.class);
                startActivityForResult(intent,100);
            }
        });
    }

    void initRecycleView(){
        mRecycleViewData=new ArrayList<>();
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecycleView.setAdapter(new CircleListPageRecycleViewAdapter(getContext(),mRecycleViewData));
        mRecycleView.addItemDecoration(new ItemDecorationImpl(getContext(),20,0));
        mRecycleView.setItemAnimator(null);
        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (isSlideToBottom(recyclerView)){
                    AbstractNetworkRequest request=new GetCirclePostRequest(getActivity())
                            .setPageIndex(pageIndex)
                            .setCurrentData(mRecycleViewData)
                            .setAdapter(mRecycleView.getAdapter());
                    request.start();
                }
            }

            boolean isSlideToBottom(RecyclerView recyclerView) {
                if (recyclerView == null) return false;
                if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
                    return true;
                return false;
            }
        });;
        pageIndex.value = 0;
        AbstractNetworkRequest request = new GetCirclePostRequest(getActivity())
                .setPageIndex(pageIndex)
                .setCurrentData(mRecycleViewData)
                .setAdapter(mRecycleView.getAdapter());
        request.start();
    }

}